package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.MessageEnum;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.common.flow.enums.FlowStateEnum;
import com.softer.wxzj.common.flow.enums.FlowTypeEnum;
import com.softer.wxzj.common.flow.util.FlowAddAuditUtil;
import com.softer.wxzj.common.flow.util.FlowAuditUtil;
import com.softer.wxzj.common.flow.util.Result;
import com.softer.wxzj.common.util.*;
import com.softer.wxzj.entity.*;
import com.softer.wxzj.mapper.*;
import com.softer.wxzj.service.TransReqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TransReqServiceImpl class
 *
 * @author wxr
 * @date 2019/7/4
 */
@Service
public class TransReqServiceImpl implements TransReqService {

    @Autowired
    private BaseOwnerInfoServiceImpl baseOwnerInfoServiceImpl;
    @Autowired
    private BaseHouseInfoMapper baseHouseInfoMapper;
    @Autowired
    private BaseOwnerInfoMapper baseOwnerInfoMapper;
    @Autowired
    private FlowAuditUtil flowUtil;
    @Autowired
    private DicContentMapper dicContentMapper;
    @Autowired
    private Result result;
    @Autowired
    private SysOperatorMapper sysOperatorMapper;
    @Autowired
    private FlowAddAuditUtil flowAddAuditUtil;
    @Autowired
    private TransReqMapper transReqMapper;
    @Autowired
    private BaseBuildInfoMapper baseBuildInfoMapper;
    @Autowired
    private OperUtil operUtil;
    @Autowired
    FlowConversionUtil flowConversionUtil;
    @Autowired
    FlowInstanceMapper flowInstanceMapper;
    @Autowired
    DepModeInfoMapper depModeInfoMapper;
    @Autowired
    BaseOwnerInfoServiceImpl baseOwnerInfoService;
    @Autowired
    private AccTransactionLogMapper accTransactionLogMapper;
    @Autowired
    private FileServiceImpl fileService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult ownerTransferList(List<BaseOwnerInfo> baseOwnerInfoList) {
        int num = NumberEnum.ZERO.getNum();
        String houseId = baseOwnerInfoList.get(0).getAccNo();
        String id = "";
        //查询房屋信息
        BaseHouseInfo baseHouseInfo = baseHouseInfoMapper.getInfo(houseId);
        BaseOwnerInfo baseOwnerInfoOld = baseOwnerInfoMapper.getInfoByAccNo(houseId);
        String oldId = baseOwnerInfoOld.getId();
        //查询旧主业主编号
        String oldOwnerNo = baseOwnerInfoMapper.selectOwnerNoForAccNo(houseId);
        //添加新主业主编号
        String rNo = baseOwnerInfoServiceImpl.ownerNo(houseId);
        TransReq transReq = new TransReq();
        //先改变旧业主的状态在增加业主
        if (NumStrEnum.TWO.getNum().equals(baseOwnerInfoList.get(0).getTransFlowState())) {
            //修改旧业主
            num = updateHouse(houseId, oldOwnerNo);
            BaseHouseInfo ba = new BaseHouseInfo();
            ba.setId(houseId);
            ba.setFlowInProgress(NumStrEnum.ZERO.getNum());
            ba.setIsAffiliated(NumStrEnum.ZERO.getNum());
            ba.setPrice(baseOwnerInfoList.get(0).getPrice());
            ba.setPurchaseDate(baseOwnerInfoList.get(0).getPurchaseDate());
            baseHouseInfoMapper.update(ba);
            if (num < 0) {
                throw new CourseException(num, "修改房屋数据操作失败");
            }
        }
        for (BaseOwnerInfo baseOwnerInfo : baseOwnerInfoList) {
            String uuid = UUIDUtil.getUUID();
            if (StringUtils.isBlank(baseOwnerInfo.getPurchaseDate())) {
                baseOwnerInfo.setPurchaseDate(null);
            }
            if (NumStrEnum.TWO.getNum().equals(baseOwnerInfoList.get(0).getTransFlowState())) {
                transReq.setRecDate(DateUtils.getDateNow());
                baseOwnerInfo.setState(NumStrEnum.ONE.getNum());
                updateOwner(baseOwnerInfo, baseHouseInfo, uuid, oldId);
            } else {
                baseOwnerInfo.setState(NumStrEnum.ZERO.getNum());
            }
            baseOwnerInfo.setOwnerNo(rNo);
            baseOwnerInfo.setPrice(baseOwnerInfoList.get(0).getPrice());
            baseOwnerInfo.setId(uuid);
            //添加付房状态
            baseOwnerInfo.setIsAffiliated(NumStrEnum.ZERO.getNum());
            baseOwnerInfo.setCreater(sysOperatorMapper.selectName(ShiroUserUtil.getUserName()).getOperName());
            baseOwnerInfoMapper.insert(baseOwnerInfo);
        }
        String newOwnerNo = baseOwnerInfoList.get(0).getOwnerNo();
        String reqId = UUIDUtil.getUUID();
        transReq.setId(reqId);
        transReq.setCreater(ShiroUserUtil.getUserName());
        transReq.setOwnerNoNew(newOwnerNo);
        transReq.setOwnerNoOld(oldOwnerNo);
        transReq.setReqNo(DepositUtil.getNo("5"));
        transReq.setHouseId(baseOwnerInfoList.get(0).getAccNo());
        transReq.setFundBal(baseHouseInfo.getFundBal());
        transReq.setAccrBal(baseHouseInfo.getAccrBal());
        transReq.setFlowState(baseOwnerInfoList.get(0).getTransFlowState());
        transReq.setRemark(baseOwnerInfoList.get(0).getTransRemark());
        num = transReqMapper.insert(transReq);
        return CommonResult.idResult(num, reqId);
    }

    /**
     * 修改过户的业主信息
     */
    private int updateOwner(BaseOwnerInfo baseOwnerInfo, BaseHouseInfo baseHouseInfo, String id, String oldId) {
        int num = 0;
        //修改交存
        if (baseOwnerInfo.getInfoType().equals(NumStrEnum.ZERO.getNum())) {
            DepModeInfo depModeInfo = depModeInfoMapper.getInfo(baseHouseInfo.getFirstMode());
            //修改初交应交的金额
            if ((NumStrEnum.ONE.getNum()).equals(depModeInfo.getDepType())) {
                baseHouseInfo = baseOwnerInfoService.changeFirst(baseHouseInfo);
            }
            num = baseHouseInfoMapper.update(baseHouseInfo);
            if (num == NumberEnum.ZERO.getNum()) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
            }
            //去掉主房id
            if (StringUtils.isNotBlank(baseHouseInfo.getMainHouseId())) {
                baseHouseInfoMapper.updateOwnerId(baseHouseInfo.getId(), null);
                if (num == NumberEnum.ZERO.getNum()) {
                    throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
                }
            }
            BigDecimal money = money(oldId, baseHouseInfo.getId());
            num = addLog(baseHouseInfo, money, oldId, "Out", "310");
            num = addLog(baseHouseInfo, money, id, "In", "311");
        }
        return num;
    }

    /**
     * 修改过户的房屋数据
     *
     * @param houseId
     * @param oldOwnerNo
     * @return
     */
    private int updateHouse(String houseId, String oldOwnerNo) {
        int num;
        //查询之前有没有过户过的业主，若有把之前的旧业主的状态改为0
        num = baseOwnerInfoMapper.selectOwnerOldOwner(houseId);
        if (num != 0) {
            baseOwnerInfoMapper.changeOwnerState(houseId, NumStrEnum.THREE.getNum(), NumStrEnum.ZERO.getNum());
        }
        //改变旧业主状态
        num = baseOwnerInfoMapper.changeState(oldOwnerNo);
        if (num <= 0) {
            return num;
        }

        return num;
    }

    @Override
    public CommonResult insert(TransReq transReq) {
        return null;
    }

    @Override
    public CommonResult update(TransReq transReq) {
        return null;
    }

    @Override
    public CommonResult delete(String id) {
        return null;
    }

    @Override
    public PageInfo<TransReq> getList(Page page, TransReq transReq) {

        //赋值区域,重要
        transReq.setFlowStateNum("1,2,3,4");
        transReq.setZoneCode(operUtil.getZoneCode());
        List<TransReq> list;
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (StringUtils.isBlank(transReq.getFuzzy())) {
            list = transReqMapper.getList(transReq);
        } else {
            list = transReqMapper.fuzzyResearch(transReq);
        }
        for (TransReq req : list) {
            req.setCreateTime(DateUtils.getDateNow());
            req.setCreater(flowInstanceMapper.selectByName(req.getId()));
        }
        PageInfo<TransReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public TransReq getInfo(String id) {
        return null;
    }

    /**
     * 查询原有业主和新业主的信息
     *
     * @param id
     * @return
     */
    @Override
    public List<List<BaseOwnerInfo>> selectOldAndNewOwner(String id) {
        List<BaseOwnerInfo> oldOwners;
        List<BaseOwnerInfo> newOwners;
        TransReq transReq = transReqMapper.getInfo(id);
        oldOwners = baseOwnerInfoMapper.selectAllOwnerForOwnerNo(transReq.getOwnerNoOld(), null);
        newOwners = baseOwnerInfoMapper.selectAllOwnerForOwnerNo(transReq.getOwnerNoNew(), null);
        //获取申请人的信息
        oldOwners.get(0).setTransRemark(transReq.getRemark());
        oldOwners.get(0).setRecDate(transReq.getRecDate());
        oldOwners.get(0).setTransFlowState(transReq.getFlowState());
        oldOwners.get(0).setTransCreater(transReq.getCreater());
        List<List<BaseOwnerInfo>> lists = new ArrayList<>();
        for (int i = 0; i < oldOwners.size(); i++) {
            oldOwners.get(i).setCertType(dicContentMapper.selectName(oldOwners.get(i).getCertType()));
        }
        for (int i = 0; i < newOwners.size(); i++) {
            newOwners.get(i).setCertType(dicContentMapper.selectName(newOwners.get(i).getCertType()));
        }
        lists.add(0, oldOwners);
        lists.add(1, newOwners);
        return lists;
    }


    /**
     * 过户审核列表
     *
     * @param page
     * @param houseAndOwner
     * @return
     */
    @Override
    public PageInfo<HouseAndOwner> selecthouseFlowState(Page page, HouseAndOwner houseAndOwner) {

        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.TRANS_REQ.getType());
        List<HouseAndOwner> list = new ArrayList<>();
        if (null != map) {
            //调用公用方法
            // 从审批流提取的业务id集合
            houseAndOwner.setId(map.get("id"));
            // 用户绑定的区域
            houseAndOwner.setZoneCode(map.get("zoneCode"));
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            if (StringUtils.isBlank(houseAndOwner.getFuzzy())) {
                list = transReqMapper.selecthouseFlowState(houseAndOwner);
            } else {
                list = transReqMapper.fuzzyResearchList(houseAndOwner);
            }
        }
        PageInfo<HouseAndOwner> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 审核
     *
     * @param businessId
     * @param opinion
     * @param mark
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult addAudit(String businessId, String opinion, String mark) {
        int i = NumberEnum.ONE.getNum();
        String auditState = flowAddAuditUtil.audit(businessId, opinion, mark,null);
        TransReq transReq = new TransReq();
        transReq.setId(businessId);
        TransReq transReq1Old = transReqMapper.getInfo(businessId);
        //审核条件封装在一个类中
        result.result(auditState, transReq);
        //不在审核中则更新业务表
        BaseHouseInfo baseHouseInfo = new BaseHouseInfo();
        if (null != auditState && !FlowStateEnum.IN_AUDIT.getState().equals(auditState)) {
            if (auditState.equals(NumStrEnum.THREE.getNum())) {
                i = auditVETOPassChangeOwner(businessId);
            } else {
                //审批通过切换业主
                i = auditPassChangeOwner(businessId);
                baseHouseInfo.setRecDate(DateUtils.getNow());
                if (i == 0) {
                    throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
                }
            }
            baseHouseInfo.setId(transReq1Old.getHouseId());
            baseHouseInfo.setFlowInProgress(NumStrEnum.ZERO.getNum());

            i = baseHouseInfoMapper.update(baseHouseInfo);
        } else if (transReq1Old.getFlowState().equals(NumStrEnum.FOUR.getNum())) {
            TransReq t = new TransReq();
            t.setId(businessId);
            t.setFlowState(NumberEnum.ONE.getNum().toString());
            i = transReqMapper.update(t);
        }
        return CommonResult.ResponseResult(i);
    }

    /**
     * 审核失败则把原业主改为状态为1，新业主为0
     */
    private int auditVETOPassChangeOwner(String id) {
        int i;
        TransReq tran = new TransReq();
        tran.setId(id);
        tran.setFlowState(NumStrEnum.THREE.getNum());
        i = transReqMapper.update(tran);
        if (i == 0) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
        }
        return i;
    }

    /**
     * 审批通过切换业主
     *
     * @param id
     * @return
     */
    private int auditPassChangeOwner(String id) {
        int i;
        //查询之前有没有过户过的业主，若有把之前的旧业主的状态改为0
        i = baseOwnerInfoMapper.selectOwnerOldOwner(transReqMapper.getInfo(id).getHouseId());
        if (i != 0) {
            baseOwnerInfoMapper.changeOwnerState(transReqMapper.getInfo(id).getHouseId(), NumStrEnum.THREE.getNum(), NumStrEnum.ZERO.getNum());
        }//审核通过旧业主状态为3，新业主状态为1
        i = baseOwnerInfoMapper.changeOwnerStateForOwnerNo(transReqMapper.getInfo(id).getOwnerNoOld(), NumStrEnum.ONE.getNum(), NumStrEnum.THREE.getNum());
        if (i == NumberEnum.ZERO.getNum()) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
        }
        i = baseOwnerInfoMapper.changeOwnerStateForOwnerNo(transReqMapper.getInfo(id).getOwnerNoNew(), NumStrEnum.ZERO.getNum(), NumStrEnum.ONE.getNum());
        if (i == NumberEnum.ZERO.getNum()) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
        }
        TransReq t = transReqMapper.getInfo(id);
        BaseOwnerInfo baseOwnerInfo = baseOwnerInfoMapper.selectOwnerInfoByOwnerNo(t.getOwnerNoNew());
        BaseHouseInfo baseHouseInfo = baseHouseInfoMapper.getInfo(transReqMapper.getInfo(id).getHouseId());
        baseHouseInfo.setPrice(baseOwnerInfo.getPrice());
        if (StringUtils.isBlank(baseOwnerInfo.getPurchaseDate())) {
            baseOwnerInfo.setPurchaseDate(null);
        }
        //去掉主房id
        if (StringUtils.isNotBlank(baseHouseInfo.getOwnerId())) {
            baseHouseInfoMapper.updateOwnerId(id, null);
            if (i == NumberEnum.ZERO.getNum()) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
            }
        }
        baseHouseInfo.setPurchaseDate(baseOwnerInfo.getPurchaseDate());
        //修改交存
        DepModeInfo depModeInfo = depModeInfoMapper.getInfo(baseHouseInfo.getFirstMode());
        //修改初交应交的金额
        if (NumStrEnum.ONE.getNum().equals(depModeInfo.getDepType())) {
            baseHouseInfo = baseOwnerInfoService.changeFirst(baseHouseInfo);
        }
        baseHouseInfoMapper.update(baseHouseInfo);
        if (i == NumberEnum.ZERO.getNum()) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
        }
        if (baseOwnerInfo.getInfoType().equals(NumStrEnum.ZERO.getNum())) {
            BaseOwnerInfo baseOwnerInfoOld = baseOwnerInfoMapper.selectOwnerInfoByOwnerNo(t.getOwnerNoOld());
            BigDecimal money = money(baseOwnerInfoOld.getId(), baseHouseInfo.getId());
            i = addLog(baseHouseInfo, money, baseOwnerInfoOld.getId(), "Out", "310");
            if (i == NumberEnum.ZERO.getNum()) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
            }
            i = addLog(baseHouseInfo, money, baseOwnerInfo.getId(), "In", "311");
            if (i == NumberEnum.ZERO.getNum()) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
            }
        }
        TransReq tran = new TransReq();
        tran.setId(id);
        tran.setFlowState(NumStrEnum.TWO.getNum());
        tran.setRecDate(DateUtils.getDateNow());
        i = transReqMapper.update(tran);
        return i;
    }

    /**
     * 计算金额
     *
     * @param ownerId
     * @param houseId
     * @return
     */
    private BigDecimal money(String ownerId, String houseId) {
        BigDecimal dep = accTransactionLogMapper.selectMoneyDepAll(houseId);
        BigDecimal req = accTransactionLogMapper.selectMoneyReqAll(houseId);
        BigDecimal exp = accTransactionLogMapper.selectMoneyExpAll(houseId);
        BigDecimal money = dep.subtract(req).subtract(exp);
        if (money.compareTo(BigDecimal.ZERO) < 1) {
            money = BigDecimal.ZERO;
        }
        return money;
    }

    /**
     * 添加log 表
     *
     * @param baseHouseInfo
     * @param money
     * @param id
     * @param type
     * @return
     */
    private int addLog(BaseHouseInfo baseHouseInfo, BigDecimal money, String id, String type, String busType) {
        AccTransactionLog accTransactionLog = new AccTransactionLog();
        accTransactionLog.setHouseId(baseHouseInfo.getId());
        accTransactionLog.setState(NumStrEnum.ONE.getNum());
        accTransactionLog.setCreater(ShiroUserUtil.getUserName());
        accTransactionLog.setBusiType(busType);
        accTransactionLog.setOwnerId(id);
        accTransactionLog.setTallyDate(DateUtils.getDateNow());
        accTransactionLog.setAccountType("325");
        accTransactionLog.setIoFlag(type);
        accTransactionLog.setPayAmount(money);
        BaseBuildInfo baseBuildInfo = baseBuildInfoMapper.getInfo(baseHouseInfo.getBuildNo());
        accTransactionLog.setBankId(baseBuildInfo.getBankCode());
        if (null == baseHouseInfo.getFundBal()) {
            baseHouseInfo.setFundBal(BigDecimal.ZERO);
        }
        if (null == baseHouseInfo.getAccrBal()) {
            baseHouseInfo.setAccrBal(BigDecimal.ZERO);
        }
        accTransactionLog.setFundBal(baseHouseInfo.getFundBal().add(baseHouseInfo.getAccrBal()));
        accTransactionLog.setAccountFrozen(baseHouseInfo.getAccountFrozen());
        accTransactionLog.setAccrBal(baseHouseInfo.getAccrBal());
        accTransactionLog.setIsEffective(NumStrEnum.ONE.getNum());
        int num = accTransactionLogMapper.insert(accTransactionLog);
        return num;
    }

    /**
     * 提交审核
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult submitAudit(String id, String arcType) {
        //校验图档必传项
        CommonResult result = fileService.checkFile(id, arcType);
        if (ShiroResultEnum.FAIL.getCode() == result.getData()) {
            return result;
        }
        int num = NumberEnum.ZERO.getNum();
        TransReq info = transReqMapper.getInfo(id);
        if (null == info) {
            return CommonResult.ResponseResult(num);
        }
        BaseHouseInfo houseInfo = baseHouseInfoMapper.getInfo(info.getHouseId());
        if (null != info && !NumStrEnum.ZERO.getNum().equals(houseInfo.getFlowInProgress())) {
            return CommonResult.strResult(NumberEnum.ZERO.getNum(), "房屋正在办理：" + flowConversionUtil.getFlowName(houseInfo.getFlowInProgress()) + "业务,不能进行申请操作");
        }
        //指定业务id和流程类型
        String initState = flowAddAuditUtil.initFlow(id, FlowTypeEnum.TRANS_REQ.getType(), null);
        //判断返回值状态 1改状态为待审核4  当为2时更新业务表为审核2通过
        TransReq transReq = new TransReq();
        transReq.setId(id);
        if (FlowStateEnum.IN_AUDIT.getState().equals(initState)) {
            //修改房屋在途业务为过户审批
            BaseHouseInfo baseHouseInfo = new BaseHouseInfo();
            baseHouseInfo.setId(houseInfo.getId());
            baseHouseInfo.setFlowInProgress(FlowTypeEnum.TRANS_REQ.getType());
            baseHouseInfoMapper.update(baseHouseInfo);
            transReq.setFlowState(NumStrEnum.FOUR.getNum());
        }
        if (FlowStateEnum.PASS.getState().equals(initState)) {
            transReq.setRecDate(DateUtils.getNow());
            transReq.setFlowState(NumStrEnum.TWO.getNum());
            //审批通过切换业主
            auditPassChangeOwner(id);
        }
        //修改业务表状态
        num = transReqMapper.update(transReq);
        return CommonResult.ResponseResult(num);
    }

    /**
     * 申请撤销
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public CommonResult applicationRevocation(String id) {
        int num;
        /**
         * 房屋状态改回去
         */
        BaseHouseInfo baseHouseInfo = baseHouseInfoMapper.getInfo(transReqMapper.getInfo(id).getHouseId());
        if (null != baseHouseInfo && ("trans_req").equals(baseHouseInfo.getFlowInProgress())) {
            num = baseHouseInfoMapper.changeFlowInProgress(NumStrEnum.ZERO.getNum(), transReqMapper.getInfo(id).getHouseId());
            if (num == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
            }
        } else {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());

        }
        num = transReqMapper.delete(id);
        //从流程删除
        num = flowInstanceMapper.deleteByBussinessId(id);

        return CommonResult.ResponseResult(num);
    }

    @Override
    public CommonResult updateOwnerNo(List<BaseOwnerInfo> baseOwnerInfos) {
        int num = NumberEnum.ZERO.getNum();
        //查询主业主信息
        String id = "";
        BigDecimal price = baseOwnerInfos.get(0).getPrice();
        for (BaseOwnerInfo baseOwnerInfo : baseOwnerInfos) {
            //更新房屋信息
            if (baseOwnerInfo.getTransFlowState().equals(NumStrEnum.TWO.getNum())) {
                if (baseOwnerInfo.getInfoType().equals(NumStrEnum.ZERO.getNum())) {
                    id = baseOwnerInfo.getId();
                    BaseOwnerInfo baseOwnerInfoOld = baseOwnerInfoMapper.getInfoByAccNo(baseOwnerInfo.getAccNo());
                    String oldId = baseOwnerInfoOld.getId();
                    TransReq oldtrans = transReqMapper.getInfo(baseOwnerInfo.getQid());
                    TransReq transReq = new TransReq();
                    if (null != oldtrans) {
                        if (oldtrans.getFlowState().equals(NumStrEnum.ZERO.getNum()) && baseOwnerInfos.get(0).getTransFlowState().equals(NumStrEnum.TWO.getNum())) {
                            updateHouse(baseOwnerInfos.get(0).getAccNo(), oldtrans.getOwnerNoOld());
                            //修改缴存
                            BaseHouseInfo base = baseHouseInfoMapper.getInfo(baseOwnerInfo.getAccNo());
                            //修改业主信息，修改房屋缴存信息
                            updateOwner(baseOwnerInfo, base, id, oldId);
                            //修改旧数据
                            BaseHouseInfo baseHouseInfo = new BaseHouseInfo();
                            baseHouseInfo.setPurchaseDate(baseOwnerInfo.getPurchaseDate());
                            baseHouseInfo.setFlowInProgress(NumStrEnum.ZERO.getNum());
                            baseHouseInfo.setIsAffiliated(NumStrEnum.ZERO.getNum());
                            baseHouseInfo.setId(baseOwnerInfo.getAccNo());
                            baseHouseInfoMapper.update(baseHouseInfo);
                            transReq.setRecDate(DateUtils.getDateNow());
                        }
                    }

                    transReq.setRemark(baseOwnerInfo.getTransRemark());
                    transReq.setUpdateTime(DateUtils.getNow());
                    transReq.setModifier(ShiroUserUtil.getUserName());
                    transReq.setId(baseOwnerInfo.getQid());
                    transReq.setFlowState(baseOwnerInfo.getTransFlowState());
                    transReqMapper.update(transReq);
                }
                baseOwnerInfo.setState(NumStrEnum.ONE.getNum());
            }
            if (StringUtils.isBlank(baseOwnerInfo.getId())) {
                baseOwnerInfo.setId(UUIDUtil.getUUID());
                baseOwnerInfo.setCreater(ShiroUserUtil.getUserName());
            }
            if (StringUtils.isBlank(baseOwnerInfo.getInfoType())) {
                baseOwnerInfo.setInfoType(String.valueOf(NumberEnum.ZERO.getNum()));
            }
            baseOwnerInfo.setPrice(price);
            num = baseOwnerInfoMapper.insertOrUpdate(baseOwnerInfo);
        }
        return CommonResult.idResult(num, baseOwnerInfos.get(0).getQid());
    }
}
